package com.example.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.Question;

@Mapper
public interface QuestionMapper extends BaseMapper<Question> {
    
    /**
     * 分页查询试题，按类型和难度
     */
    @Select("<script>" +
            "SELECT * FROM questions WHERE 1=1 " +
            "<if test='type != null and type != \"\"'> AND type = #{type} </if>" +
            "<if test='difficulty != null'> AND difficulty = #{difficulty} </if>" +
            "<if test='knowledgePoint != null and knowledgePoint != \"\"'> AND knowledge_point = #{knowledgePoint} </if>" +
            "ORDER BY create_time DESC" +
            "</script>")
    IPage<Question> findQuestionsByCondition(Page<Question> page, 
            @Param("type") String type, 
            @Param("difficulty") Integer difficulty,
            @Param("knowledgePoint") String knowledgePoint);
            
    /**
     * 统计符合条件的试题总数
     */
    @Select("<script>" +
            "SELECT COUNT(*) FROM questions WHERE 1=1 " +
            "<if test='type != null and type != \"\"'> AND type = #{type} </if>" +
            "<if test='difficulty != null'> AND difficulty = #{difficulty} </if>" +
            "<if test='knowledgePoint != null and knowledgePoint != \"\"'> AND knowledge_point = #{knowledgePoint} </if>" +
            "</script>")
    Long countQuestions(@Param("type") String type, 
            @Param("difficulty") Integer difficulty,
            @Param("knowledgePoint") String knowledgePoint);
} 